package com.zszq.ms.bigmodeltest.service.impl;

import cn.hutool.core.io.FileUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zszq.ms.bigmodeltest.domain.response.Result;
import com.zszq.ms.bigmodeltest.entity.AiFile;
import com.zszq.ms.bigmodeltest.mapper.AiFileMapper;
import com.zszq.ms.bigmodeltest.service.AiFileService;
import com.zszq.ms.bigmodeltest.utils.AlyOSSUtil;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Date;
import java.util.UUID;

/**
 * 文件服务实现类
 *
 * @author ms
 * @since 2025-04-02
 */
@Service
public class AiFileImpl extends ServiceImpl<AiFileMapper, AiFile> implements AiFileService {

    @Override
    public Result upload(MultipartFile file) {
        String url;
        try {
            url = AlyOSSUtil.uploadFile(file.getInputStream());
        } catch (IOException e) {
            return Result.failure("上传失败");
        }
        String originalFilename = file.getOriginalFilename();
        String type = FileUtil.extName(originalFilename);
        AiFile aiFile = new AiFile();
        aiFile.setId(UUID.randomUUID().toString());
        aiFile.setName(originalFilename);
        aiFile.setUrl(url);
        aiFile.setType(type);
        aiFile.setSize(file.getSize());
        Date now = new Date();
        aiFile.setCreateTime(now);
        aiFile.setUpdateTime(now);
        baseMapper.insert(aiFile);
        return Result.success(url);
    }
}
